import java.awt.*; 

import java.applet.*;

import java.awt.image.*;

/** GAME BALAP MOBIL

* Created By :

*  Kelompok 1

* Ligar Sekar Wangi Ligar89@multiply.com

* Tri Maryani		

* Winda Rahadyan 	Cepearl@multiply.com*/



public class Car extends Applet implements Runnable 

{

Image img,car1,cup; 


// Untuk menentukan posisi mobil 

static int position=235; 

static int points = 0; 



// Untuk menentukan kecepatan mobil

static int delay = 80; 

road rd; 

Thread thr; 

static int pts=30; 

boolean msg=true; 



static boolean kill=false; 

public void init() 

{ 

int x[] = { 15, 15, 0, 60, 45, 45 }; 

int y[] = { 45, 50, 58, 58, 50, 45 }; 

setBackground(Color.black);



// Menggambar Mobil

img = createImage(60,60); 

Graphics g = img.getGraphics(); 

g.setColor(Color.black); 

g.fillRect(0,0,60,60); 

g.setColor(Color.green); 

g.fillRect(12,20,36,7); 

g.fillRect(8,15,4,17); 

g.fillRect(48,15,4,17); 

g.fillRect(5,40,50,7); 

g.fillRect(0,35,5,17); 

g.fillRect(55,35,5,17);

g.setColor(Color.red); 

g.fillRect(20,20,20,15); 

g.fillRect(15,15,30,40); 

g.setColor(Color.blue); 

g.fillRect(20,20,7,10); 

g.fillRect(33,20,7,10); 

g.setColor(Color.red); 

g.fillRect(35,35,3,6); 

g.setFont(new Font("TimesRoman",Font.PLAIN,7)); 

g.setColor(Color.white); 

g.fillPolygon(x,y,6); 

g.setColor(Color.black); 

g.drawString(" LIGAR ",15,52);



// Menggambar mobil

car1 = createImage(60,60); 

Graphics g1 = car1.getGraphics(); 

g1.setColor(Color.black); 

g1.fillRect(0,0,60,60); 

g1.setColor(Color.green); 

g1.fillRect(12,20,36,7); 

g1.fillRect(8,15,4,17); 

g1.fillRect(48,15,4,17); 

g1.fillRect(5,40,50,7); 

g1.fillRect(0,35,5,17); 

g1.fillRect(55,35,5,17);

g1.setColor(Color.blue); 

g1.fillRect(20,20,20,15); 

g1.fillRect(15,15,30,40); 

g1.setColor(Color.red); 

g1.fillRect(20,20,7,10); 

g1.fillRect(33,20,7,10); 

g1.setColor(Color.blue); 

g1.fillRect(35,35,3,6); 

g1.setFont(new Font("Verdana",Font.PLAIN,7)); 

g1.setColor(Color.white); 

g1.fillPolygon(x,y,6); 

g1.setColor(Color.black); 

g1.drawString("WindaTri ",15,52);

thr = new Thread(this); thr.start(); rd = new road(getGraphics(),car1,this); rd.start();




// Cup Image 

int a[] = {20,5,35}; 

int b[] = {150,160,160}; 

cup = createImage(50,165); 

Graphics handle = cup.getGraphics(); 

handle.setColor(Color.black); 

handle.fillRect(0,0,50,165); 

handle.setColor(Color.red); 

handle.fillArc(0,40,40,30,0,180); 

handle.setColor(Color.yellow); 

handle.fillArc(0,15,40,80,180,180); 

handle.setColor(Color.red); 

handle.drawLine(20,95,20,150); 

handle.fillPolygon(a,b,3); 

} 


public void run() 

{



while(points <= 52) 

{ 

if(points == 52 || kill == true) 

{ 

rd.stop(); 

repaint(); 

thr.stop(); 

} 


if((points%4)==0) 

{ 

rd.j = 0; 

pts = points; 

points++; 

delay--; 

if(delay <= 0) 

delay = 0; 

rd.flag=1; 

repaint(); 

}

try 

{ 

Thread.sleep(delay); 

}catch(InterruptedException exp){}

} 

} 

public void destroy() 

{ 

thr.stop(); 

rd.stop(); 

}



public boolean mouseDown(Event e , int x, int y) 

{ 

if(e.id == Event.MOUSE_DOWN) 

{ 

if(position == 235) 

position = 355; 

else 

position = 235; 

repaint(); 

}

return true; 

} 

public void paint(Graphics gr) 

{ 

if(!kill) 

{ 

if(msg) 

{ 



// Pesan pembuka

gr.setColor(Color.black); 

gr.fillRect(0,0,640,400); 

gr.setColor(Color.pink); 

gr.setFont(new Font("comicsans",Font.BOLD,16)); 

gr.drawString("                TUGAS APLET JAVA                                         ",140,100); 

gr.drawString("                GAME BALAP MOBIL                                         ",140,130);

gr.drawString("                  CREATED BY :                                           ",140,160);

gr.drawString("            KELOMPOK 1 (aoi.purple.star)                                ",140,200); 

gr.drawString(" 1. Ligar Sekar Wangi  (10751000204)  Ligar89@multiply.com                ",140,280); 

gr.drawString(" 2. Tri Maryani        (10751000243)  triya3389@multiply.com                 ",140,320);

gr.drawString(" 3. Winda Rahadyan     (10751000044)  cepearl89@multiply.com                 ",140,360);

msg = false; 

try{ 

Thread.sleep(3000); 

}catch(Exception exp){} 

gr.setColor(Color.black); 

gr.fillRect(0,0,640,400); 

} 

gr.setColor(Color.white); 

gr.fillRect(200,0,10,400); 

gr.fillRect(440,0,10,400); 

gr.drawImage(img,position,300,this); 

gr.setColor(Color.yellow); 

gr.fillRect(550,5,637,25); 

gr.setColor(Color.blue); 

gr.setFont(new Font("TimesRoman",Font.BOLD,20)); 

gr.drawString("Score :"+pts,557,22); 

if(points >= 52) 

{

for(int xyz=0;xyz<3;xyz++) 

{ 

gr.setColor(Color.yellow); 

gr.drawString("SELAMAT ANDA MENANG (^O^)d",240,100); 

gr.setFont(new Font("TimesRoman",Font.BOLD,20)); 

gr.drawString("Score :50",557,22); 

try 

{ 

Thread.sleep(500); 

}catch(InterruptedException exp){} 

} 

} 

} 

else 

{ 

gr.setColor(Color.yellow); 

gr.drawString("ANDA KALAH ^_^ ",250,200); 

} 

} 

}

class road extends Thread 

{ 

int i; 

public static int j = 0; 

Graphics g; 

Image car2; 

ImageObserver io; 

public static int flag = 0; 

boolean msg=true; 

road(Graphics g,Image car2,ImageObserver io) 

{ 

this.g = g; 

this.io = io; 

this.car2 = car2; 

} 

public void run() 

{ 

drawRoad(g); 

}



public void drawRoad(Graphics gr) 

{ 

if(msg) 

{ 

gr.setColor(Color.black); 

gr.fillRect(0,0,640,400); 

gr.setColor(Color.yellow); 

gr.setFont(new Font("TimesRoman",Font.BOLD,16)); 

gr.drawString("Klik...untuk memulai game, gunakan 'mouse-click' untuk menjalankan mobil",140,50); 



msg = false; 

try

{

Thread.sleep(3000); 

}catch(Exception exp){} 

gr.setColor(Color.black); 

gr.fillRect(0,0,640,400); 

} 

for(;j<=1000;j+=10) 

{ 

for(i=-1000;i<=479;i+=60) 

{ 

gr.setColor(Color.black); 

gr.fillRect(320,i+j,10,i+j+50); 

gr.setColor(Color.white); 

gr.fillRect(320,i+j+10,10,i+j+60); 

} 

gr.clearRect(235,j-10,60,60); 

gr.drawImage(car2,235,0+j,io); 

gr.clearRect(355,-150+(j-10),60,60); 

gr.drawImage(car2,355,-150+j,io); 

gr.clearRect(235,-300+(j-10),60,60); 

gr.drawImage(car2,235,-300+j,io); 

gr.clearRect(355,-450+(j-10),60,60); 

gr.drawImage(car2,355,-450+j,io); 

if( (Car.position == 235 && (j >= 250 && j <= 360)) || (Car.position == 355 && (j >= 400 && j <= 510)) || (Car.position == 235 && (j >= 550 && j <= 660)) || (Car.position == 355 && (j >= 700 && j <= 810)) ) 

{ 

try 

{ 

Thread.sleep(2000); 

Car.kill = true; 

}catch(InterruptedException exp){} 

} 

if (j >= 360 ) { if( (( j - 360 ) % 150 ) == 0 ) 

{ 

if(flag == 1) 

{ 

Car.points--; 

flag = 0; 

} 

Car.points++; 

gr.setColor(Color.yellow); 

gr.fillRect(550,5,637,25); 

gr.setColor(Color.blue); 

gr.setFont(new Font("TimesRoman",Font.BOLD,20)); 

gr.drawString("Score :"+Car.points,557,22); 

} 

} 

try 

{ 

Thread.sleep(Car.delay); 

}catch(InterruptedException exp){} 

} 

} 

} 
